import types from "../action-types";
import {reqVerifyAuthToken} from "../../api/login";


const startLoggedUser = () => {
    return {
        type: types.START_LOGGED_USER,
        token: "",
        pending: true,
        logged: false,
    }
};
const successLoggedUser = (userInfo, token) => {
    return {
        type: types.SUCCESS_LOGGED_USER,
        token: token,
        pending: false,
        logged: true,
    }
};
const failedLoggedUser = () => {
    return {
        type: types.FAILED_LOGGED_USER,
        token: "",
        pending: true,
        logged: false,
    }
};


export const verifyAuthToken = (data) => (dispatch) => {
    return new Promise((resolve, reject) => {
        dispatch(startLoggedUser());
        if (data) {
            reqVerifyAuthToken({token: data}).then((response) => {
                if (response.code === 200) {
                    dispatch(successLoggedUser(response.data, response.data.token))
                } else {
                    dispatch(failedLoggedUser());
                }
            });
        } else {
            dispatch(failedLoggedUser());
        }
    });

};
